package com.ciwong.epaper.common.dialogbottom;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.adapter.CheckGradeDialogAdapter;
import com.ciwong.epaper.modules.me.adapter.CheckPublishDialogAdapter;
import com.ciwong.epaper.modules.me.bean.MaterialBean;
import com.ciwong.epaper.modules.me.bean.MaterialClassificationBean;
import com.ciwong.epaper.modules.me.bean.SubjectBean;
import com.ciwong.epaper.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ChackGradeBottomDialog extends BaseBottomDialog {
    private RecyclerView rvDialogGrade;
    private RecyclerView rvDialogPublish;

    private ItemCallBack itemCallBack;
    private CheckGradeDialogAdapter checkGradeDialogAdapter;
    private CheckPublishDialogAdapter checkPublishDialogAdapter;
    private List<MaterialClassificationBean> gradeList = new ArrayList<>();
    private String LAST_SEMESTER = "上册";
    private String NEXT_SEMESTER = "下册";
    private String ALL_SEMESTER = "全册";

    public interface ItemCallBack {
        void onItemCallBack(List<MaterialBean> materialBeans, int position);
    }

    public void setOnItemCallBackListener(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public ChackGradeBottomDialog(Context context, int style) {
        super(context, style);
        gradeList = new ArrayList<>();
    }

    @Override
    public void initView(Context context) {
        initViewClick();
        initRecycle(context);
    }


    public void initViewClick() {
        View mRootView = LayoutInflater.from(this.getContext()).inflate(R.layout.fragment_check_grade_dialog, null);
        mRootView.findViewById(R.id.tl_close).setOnClickListener(this);
        rvDialogGrade = mRootView.findViewById(R.id.rv_dialog_grade);
        rvDialogPublish = mRootView.findViewById(R.id.rv_dialog_publish);
        setContentView(mRootView);
    }

    public void initRecycle(Context context) {
        if (checkGradeDialogAdapter == null) {
            checkGradeDialogAdapter = new CheckGradeDialogAdapter(context);
        }
        if (rvDialogGrade == null) return;
        rvDialogGrade.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvDialogGrade.setAdapter(checkGradeDialogAdapter);
        rvDialogGrade.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL, dpToPixel((float) 0.5, context), R.color.color_e6e6e6));
        checkGradeDialogAdapter.setOnItemClickListener(new CheckGradeDialogAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, List<MaterialClassificationBean> list, int position) {
                checkPublishDialogAdapter.setData(list.get(position).getMaterialBeanList());
            }
        });
        if (checkPublishDialogAdapter == null)
            checkPublishDialogAdapter = new CheckPublishDialogAdapter(context);
        if (rvDialogPublish == null) return;
        rvDialogPublish.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvDialogPublish.setAdapter(checkPublishDialogAdapter);
        rvDialogPublish.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL, dpToPixel((float) 0.5, getContext()), R.color.color_e6e6e6));

        checkPublishDialogAdapter.setOnItemClickListener(new CheckPublishDialogAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, List<MaterialBean> list, int position) {
                dismiss();
                if (itemCallBack != null) {
                    itemCallBack.onItemCallBack(list, position);
                }
            }
        });
    }

    public void initData(SubjectBean subjectBean) {
        if (gradeList == null) gradeList = new ArrayList<>();
        gradeList.clear();
        List<MaterialBean> material = subjectBean.getMaterial();
        Collections.sort(material);//先排序
        MaterialClassificationBean materialClassificationBean;
        String gradeNamePlus = null;
        List<MaterialBean> materialBeanList = null;
        for (int i = 0; i < material.size(); i++) {
            MaterialBean materialBean = material.get(i);
            if (materialBean.isClassified()) {
                materialBean.setIsClassified(false);
                continue;
            }
            materialBeanList = new ArrayList<>();
            materialClassificationBean = new MaterialClassificationBean();
            materialClassificationBean.setGradeId(materialBean.getGradeId());
            materialClassificationBean.setSemester(materialBean.getSemester());
            //当为选中状态时才加入此参数
            if (materialBean.isSelected())
                materialClassificationBean.setSelect(materialBean.isSelected());
            if (materialBean.getSemester() == MaterialClassificationBean.LAST_SEMESTER) {
                gradeNamePlus = materialBean.getGradeName() + LAST_SEMESTER;
            } else if (materialBean.getSemester() == MaterialClassificationBean.NEXT_SEMESTER) {
                gradeNamePlus = materialBean.getGradeName() + NEXT_SEMESTER;
            } else if (materialBean.getSemester() == MaterialClassificationBean.ALL_SEMESTER) {
                gradeNamePlus = materialBean.getGradeName() + ALL_SEMESTER;
            } else {
                gradeNamePlus = materialBean.getGradeName();
            }
            materialClassificationBean.setGradeName_plus(gradeNamePlus);
            materialBeanList.add(materialBean);
            for (int j = i + 1; j < material.size(); j++) {//一次循环 筛选出 第一个一级目录的子数据
                MaterialBean materialBean_in = material.get(j);
                if (materialBean_in.getGradeId() == materialBean.getGradeId()
                        && materialBean_in.getSemester() == materialBean.getSemester()) {
                    materialBean_in.setIsClassified(true);
                    if (materialBean_in.isSelected())
                        materialClassificationBean.setSelect(materialBean_in.isSelected());
                    materialBeanList.add(materialBean_in);
                }
            }
            materialClassificationBean.setMaterialBeanList(materialBeanList);
            gradeList.add(materialClassificationBean);
        }
        if (checkGradeDialogAdapter != null) checkGradeDialogAdapter.setData(gradeList);
        for (int i = 0; i < gradeList.size(); i++) {
            MaterialClassificationBean gradeBean = gradeList.get(i);
            if (gradeBean.isSelect())
                if (checkPublishDialogAdapter != null)
                    checkPublishDialogAdapter.setData(gradeBean.getMaterialBeanList());
        }
    }
}
